package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.Equipment;
import com.mesSystem.entity.Process;
import com.mesSystem.service.impl.ProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("process")
public class ProcessController {
    @Autowired
    ProcessService processService;


    @PostMapping("increase")
    public Result increase(@RequestBody Process process) {
        try {
            processService.save(process);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("工序似乎已存在");
        }

        return Result.ok("创建成功");
    }

    /**
     * 更新操作（名字暂不可更新）
     * @param process
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody Process process) {
        try {
            log.info(process.toString());
            processService.updateById(process);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("工序名不可更新（其他错误请联系管理员）");
        }
        return Result.ok("更新成功");
    }


    /**
     * 分页查询信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 创建分页器对象
        Page<Process> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        processService.page(Page, new QueryWrapper<Process>().orderByAsc("process_id"));

        // 从分页器对象中获取分页后的数据
        List<Process> list = Page.getRecords();

        log.info("被查询信息为：{}", list);

        // 返回分页结果
        return Result.ok(list);
    }

    @DeleteMapping("delete")
    public Result delete(Long processId) {
        processService.removeById(processId);
        return Result.ok("删除成功");
    }


}
